local lsp_ranyan = fk.CreateSkill {
    name = "lsp_ranyan",
}

Fk:loadTranslationTable {
    ["lsp_ranyan"] = "燃焰",
    [":lsp_ranyan"] = "每当你使用与上一张由你使用的牌点数、类型相同的牌时，你可以摸X张牌，或获得其他角色一张手牌；若此时是你的回合，结束阶段，若你因此获得的牌总数超过存活角色数，你失去一点体力(X为相同的项)。",

    ["@lsp_ranyan"] = "燃焰",
    ["@lsp_ranyan_num"] = "燃",
}
lsp_ranyan:addEffect(fk.CardUsing, {
    mute = true,
    can_trigger = function(self, event, target, player, data)
        return target == player and player:hasSkill(lsp_ranyan.name) and data.card
    end,
    on_cost = Util.TrueFunc,
    on_use = function(self, event, target, player, data)
        local room = player.room
        if player:getMark("@lsp_ranyan") ~= 0 then
            local last_card_number = player:getMark("@lsp_ranyan")[1]
            local last_card_type = player:getMark("@lsp_ranyan")[2]
            local n = 0
            local cards = 0
            if data.card.number == last_card_number then
                n = n + 1
            end
            if data.card:getTypeString() .. "_char" == last_card_type then
                n = n + 1
            end
            if n > 0 then
                room:notifySkillInvoked(player, lsp_ranyan.name, "drawcard")
                local to = room:askToChoosePlayers(
                    player,
                    {
                        targets = table.filter(room:getOtherPlayers(player),
                            function(p) return p:getHandcardNum() > 0 end),
                        min_num = 1,
                        max_num = 1,
                        prompt = "燃焰：是否选择获得一名角色的一张手牌，点击取消摸牌",
                        skill_name = lsp_ranyan.name,
                    }
                )
                if #to == 0 then
                    local ids = player:drawCards(n, lsp_ranyan.name)
                    cards = cards + #ids
                end
                if #to > 0 then
                    local id = room:askToChooseCard(player, { target = to[1], flag = "h", skill_name = lsp_ranyan.name })
                    room:obtainCard(player, id, false, fk.ReasonPrey, player)
                    cards = cards + 1
                end
            end
            if n > 0 and room.current == player then
                room:addPlayerMark(player, "@lsp_ranyan_num", cards)
            end
        end
        room:setPlayerMark(player, "@lsp_ranyan", { data.card.number, data.card:getTypeString() .. "_char" })
    end,
})
lsp_ranyan:addEffect(fk.EventPhaseStart, {
    mute = true,
    is_delay_effect = true,
    can_trigger = function(self, event, target, player, data)
        return target == player and player.phase == Player.Finish and player:getMark("@lsp_ranyan_num") > 0
    end,
    on_cost = Util.TrueFunc,
    on_use = function(self, event, target, player, data)
        local room = player.room
        if player:getMark("@lsp_ranyan_num") > #room.alive_players then
            room:notifySkillInvoked(player, lsp_ranyan.name, "defensive")
            room:loseHp(player, 1, lsp_ranyan.name)
        end
        room:setPlayerMark(player, "@lsp_ranyan_num", 0)
    end,
})

return lsp_ranyan
